import java.io.*;
import java.util.*;
import java.text.*;

public class AnalyzeFiles4 {

	static PrintWriter out;
	static PrintWriter outDiff;
	static long save = 0;
	static String priority_dir="oct-2011";

	public static void main(String[] args) throws Exception {
		out = new PrintWriter(new File("files1.log"));
		outDiff = new PrintWriter(new File("rm1_files.sh"));
		StringBuffer _dirs_out2 = new StringBuffer("");
		BufferedReader _br = new BufferedReader(new InputStreamReader(
				new FileInputStream("dirs1.sort.log")));
		List<String> dirs = new ArrayList<String>();
		String _li = "";
		while ((_li = _br.readLine()) != null) {
			_dirs_out2.append(_li);
			if (_li.startsWith("meld \"")) {
				int _o = _li.indexOf("\" \"");
				dirs.add(_li.substring(6, _o));
				dirs.add(_li.substring(_o + 3, _li.length() - 1));
			}
		}
		_br.close();
		out.println("-----------------");
		String dirs_out2 = _dirs_out2.toString();

		File _dir = new File(".");
		File[] fls = _dir.listFiles();

		TreeMap<Long, List<String>> filemap = new TreeMap<Long, List<String>>();
		int minsz = 100000;
		filemap.clear();
		for (File f : fls) {
			if (f.getName().endsWith(".f1.txt")) {

				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(f)));
				String line = "";
				while ((line = br.readLine()) != null) {
					line=decrypt(line);
					int i = line.lastIndexOf("::");
					Long size = Long.parseLong(line.substring(i + 2));
					if (size < minsz)
						continue;
					String _file = line.substring(0, i);
					boolean _f = false;
					for (String _d : dirs) {
						if (_file.startsWith(_d + "/")) {
							_f = true;
							break;
						}
					}
					if (_f)
						continue;
					size = 999999999999999L - size;
					List<String> _files = filemap.get(size);
					if (_files == null)
						_files = new ArrayList<String>();
					_files.add(_file);
					filemap.put(size, _files);
				}

				Set<Long> keys = filemap.keySet();
				for (Long key : keys) {
					List<String> vals = filemap.get(key);
					if (vals.size() > 1) {
						String prev_val = "";
						for (String val : vals) {
							out.println(val);
							if (prev_val.length() > 0){
								if((priority_dir !=null) && (priority_dir.length()>0)){
									if( (val.indexOf(priority_dir)>-1) && (prev_val.indexOf(priority_dir)>-1)){
									}else {
										if(  (val.indexOf(priority_dir)>-1)){
											outDiff.println("echo \"" + val + "\" \""
													+ prev_val + "\"");
											outDiff.println("diff \"" + val + "\" \""
												+ prev_val + "\"");
											outDiff.println("rm -i \""
													+ prev_val + "\"");
											prev_val=val;
										}
										else {
											outDiff.println("echo \"" + prev_val + "\" \""
													+ val + "\"");
											outDiff.println("diff \"" + prev_val + "\" \""
												+ val + "\"");
											outDiff.println("rm -i \""
													+ val + "\"");
										}
									}
								}else{
									outDiff.println("echo \"" + prev_val + "\" \""
											+ val + "\"");
									outDiff.println("diff \"" + prev_val + "\" \""
										+ val + "\"");
									outDiff.println("rm -i \""
									+ val + "\"");
								 
								}
								
							}else 
								prev_val = val;
							save += ((999999999999999L - key) * (vals.size() - 1));
						}
					}
				}

			}
		}
		out.println("\n\nSAVINGS=" + save);
		out.close();
		outDiff.close();
	}

	
	public static void storeProperties(Properties props) throws Exception{
		ByteArrayOutputStream bao= new ByteArrayOutputStream();
		props.storeToXML(bao,"modified-"+new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(new Date()));
		byte[] barr=bao.toByteArray();
		FileOutputStream fos=new FileOutputStream(".dir.txt");
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Crypto.getInstance().encrypt(bis,fos);
		fos.close();
		bis.close();
		bao.close();

	}
	public static Properties loadProperties() throws Exception{
		ByteArrayOutputStream bao=new ByteArrayOutputStream();
		FileInputStream fis=new FileInputStream(".dir.txt");
		Crypto.getInstance().decrypt(fis, bao);
		byte[] barr=bao.toByteArray();
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Properties props= new Properties();
		props.loadFromXML(bis);
		bao.close();
		fis.close();
		return props;
	}
	
	public static void writeFile(String FileName, String content) throws Exception{
		FileOutputStream fos=new FileOutputStream(FileName);
		byte[] barr=content.getBytes();
		ByteArrayInputStream bis= new ByteArrayInputStream(barr);
		Crypto.getInstance().encrypt(bis,fos);
		fos.close();
		
	}
	public static String readFile(String FileName) throws Exception{
		ByteArrayOutputStream bao=new ByteArrayOutputStream();
		FileInputStream fis=new FileInputStream(FileName);
		Crypto.getInstance().decrypt(fis, bao);
		String ret=new String(bao.toByteArray());
		bao.close();
		fis.close();
		return ret;
		
	}
	
	public static String encrypt(String str){
		return Crypto.getInstance().encrypt(str);
	}
	public static String decrypt(String str){
		return Crypto.getInstance().decrypt(str);
	}

}
